<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 *  SearchCustomers_model Model
 *
 */

class SearchCustomers_model extends MY_Model{

	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_contacts';
	}

        public function searchCustomers($name=NULL,$phone=NULL){
            
            $this->db->select('c.customerID,c.name,c.address,c.phone,c.social,DATE_FORMAT(c.birthday,("%d-%m-%Y")) AS birthday,c.info_bill,c.note,c.lastupdated,c.lastBooking,c.totalMoneyBooking,c.surplus,c.salesID,t.name AS type,ag.pcc AS agent_code,prov.name AS province,usr.username AS username')
                ->from('book_contacts AS c')
                ->join('book_type_customers AS t','t.id = c.type')
                ->join('book_agent_code AS ag','c.agent_code = ag.id')
                ->join('book_province AS prov','prov.id = c.province')
                ->join('users AS usr','usr.id = c.owner');
            
            if($name){
                 $this->db->where('c.name REGEXP ("'.$name.'")');
            }
            if($phone){
                $this->db->like('c.phone',$phone);
            }
            $query = $this->db->get();
            return $query->result();
        }
}